import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-03-16
 * Time: 16:44
 */
public class JDBCDemo5 {
    public static void main(String[] args) throws SQLException {
        /**
         * 查询表中的内容
         */
        //1.建立数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java111?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("11111111");
        //2.建立连接
        Connection connection=dataSource.getConnection();
        //3.构造一个操作数据库的sql
        String sql="select * from student";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //4.执行sql
        ResultSet resultSet=preparedStatement.executeQuery();
        //5.遍历结果集合
        while(resultSet.next()){
            int id=resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id= "+id+",name="+name);
        }
        //6.释放资源
        resultSet.close();
        preparedStatement.close();
        connection.close();



    }
}
